package com.appspot.zcmjqm.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.appspot.zcmjqm.app.model.Product;
import com.appspot.zcmjqm.service.ProductService;

@Controller
@RequestMapping("/product")
public class ProductControllerWebMVCImpl implements ProductController {
	
	@Autowired
	private ProductService productService;

	@RequestMapping("/display.do")
	public ModelAndView displayProduct(@RequestParam String productKey) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("product", productService.getById(productKey));
		mav.setViewName("product");
		return mav;
	}

	@RequestMapping("/list.do")
	public ModelAndView displayProducts(@RequestParam String categoryKey) {
		ModelAndView mav = new ModelAndView();
		List<Product> products = productService.getByCategory(categoryKey);
		mav.addObject("products", products);
		mav.setViewName("products");
		return mav;
	}

	public void setProductService(ProductService productService) {
		this.productService = productService;
	}

}